package com.gutsyzhan.activitifly;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

@SpringBootTest
public class Part1_Deployment {
    @Resource
    private RepositoryService repositoryService;

    /**
     * 通过BPMN部署流程
     */
    @Test
    public void initDeploymentBPMN(){
        String fileName = "BPMN/Part1_Deployment.bpmn20.xml";
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource(fileName)
                .name("流程部署测试BPMN")
                .deploy();
        System.out.println(deployment.getName());
    }

    /**
     * 通过ZIP部署流程
     */
    @Test
    public void initDeploymentZIP(){
        String fileName = "BPMN/Part1_Deployment.bpmn20V2.zip";
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        Deployment deployment = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .name("流程部署测试ZIP")
                .deploy();
        System.out.println(deployment.getName());
    }

    /**
     * 查询流程部署
     */
    @Test
    public void getDeployments(){
        List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
        for (Deployment deployment : deployments) {
            System.out.println("Id:" + deployment.getId());
            System.out.println("Key:" + deployment.getKey());
            System.out.println("Name:" + deployment.getName());
            System.out.println("DeploymentTime:" + deployment.getDeploymentTime());
            System.out.println();
        }
    }
}
